﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System.Collections.Generic;
using System.Linq;
using System.Text;

#endregion

namespace ScrumTable.Common.Collections
{
    /// <summary>
    /// Specifies a chain of keys which corresponds, when joined, to a unqiue value.
    /// </summary>
    /// <remarks>
    /// A chain is formated as follows:
    ///  Id1 # Id2 # Id2
    ///   OR
    ///  Id1.1 # Id1.2 # Id1.2, Id2.1 # Id2.2 # Id2.2
    /// </remarks>
    public class MultiValueChain<T> : ValueChain<T>
        where T : class
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the internal chain separator.
        /// </summary>
        internal static readonly string[] ChainSeparator = new[] { ", " };

        private readonly IList<ValueChain<T>> _multiChain;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Returns the additional stored chain entries in this MultiChain
        /// instance.
        /// </summary>
        public ICollection<ValueChain<T>> MultiChain
        {
            get { return new ReadOnlyCollection<ValueChain<T>>(_multiChain); }
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new ValueChain instance with the specified data.
        /// </summary>
        public MultiValueChain(IEnumerable<ValueChain<T>> chains)
            : base(chains != null && !chains.IsEmpty() ? ((IEnumerable<T>)chains.First()) : new T[0])
        {
            _multiChain = (chains != null) ? chains.ToArray() : new ValueChain<T>[0];
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------
        
        /// <summary>
        /// Serializes the current instance into its string representation.
        /// </summary>
        /// <returns>Returns the serialized data.</returns>
        internal override string Serialize()
        {
            StringBuilder chain = new StringBuilder();

            MultiChain.ForEach(
                mc =>
                {
                    var serializedData = mc.Serialize();

                    if (!string.IsNullOrEmpty(serializedData))
                    {
                        if (chain.Length > 0)
                        {
                            chain.Append(ChainSeparator[0]);
                        }
                        chain.Append(serializedData);
                    }
                });
            return chain.ToString();
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}